﻿#include "userinf.h"
#include "ui_userinf.h" 
#include "odbclib.h"
userinf::userinf(int sex,QString realname,QString id,QString typewrite,QWidget *parent) :
    sex(sex),id(id),realname(realname),typewrite(typewrite),
    QWidget(parent),
    ui(new Ui::userinf)
{

    ui->setupUi(this);

    setWindowTitle(QString::fromLocal8Bit("个人信息"));
   this->setWindowIcon(QIcon(":/photo/11.jfif"));

    //换皮肤
    setfacer(4);

    //填充信息
    ui->id->setText(id);
    ui->realname->setText(realname);

  if(sex==1)
   {
       ui->sexedit->setText(QString::fromLocal8Bit("男"));
   }
    else if(sex==2)
    {
        ui->sexedit->setText(QString::fromLocal8Bit("女"));
    }
  ui->typewrite->setText(typewrite);
}

userinf::~userinf()
{
    delete ui;
}

void userinf::setid(QString id)
{
    this->id=id;
}

void userinf::setname(QString name)
{
    this->realname=name;
}

void userinf::settypewrite(QString typewrite)
{
    this->typewrite=typewrite;
}

void userinf::setsex(int sex)
{
    this->sex=sex;
}

void userinf::setfacer(int index)
{
    //换背景
    QImage _image;   //创建image
   //利用字符串拼接 完成photo
    QString str=QString(":/photo/back%1.jpg").arg(index);
   _image.load(str);
   setAutoFillBackground(true);   // 这个属性一定要设置
    QPalette pal(palette());
     pal.setBrush(QPalette::Window, QBrush(_image.scaled(size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    setPalette(pal);
}

void userinf::on_update_clicked()
{
    //开始修改用户列表 信息  目的将所有的控件变成可写入
    ui->realname->setReadOnly(false);
    ui->sexedit->setReadOnly(false);
    ui->typewrite->setReadOnly(false);
}


void userinf::on_save_clicked()
{
    //获取控件上的属性值
    QString id=ui->id->text();
    QString realname=ui->realname->text();
    QString typewrite=ui->typewrite->toPlainText();
    QString sexstr=ui->sexedit->text();
    int sexint=1;
    if(sexstr=="女"){
        sexint=2;
    }
    //保存修改到数据库中
    ODBClib *o=new ODBClib;
    QSqlDatabase db=o->connectdb();
     QSqlQuery qsQuery=QSqlQuery(db);

    QString str=QString("UPDATE myqicq_user SET name='%1', sex=%2,typewrite='%3' WHERE id='%4';").arg(realname).arg(sexint).arg(typewrite).arg(id);
    qsQuery.exec(str);
    qsQuery.first();
    QMessageBox::information(NULL,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("修改成功！！！"));
}

